package com.example.demo.controller;

import com.example.demo.pojo.Account;
import com.example.demo.pojo.Staff;
import com.example.demo.service.AccountService;
import com.example.demo.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private StaffService staffService;

    @RequestMapping("/user/login")
    public String userLogin(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            HttpSession session,
                            Model model){
        Account account = accountService.userLogin(username,password);
        if(account != null){
            session.setAttribute("loginUser",username);
            Staff staff = staffService.getStaff(account.getS_id());
            session.setAttribute("loginuser",staff);
           // model.addAttribute("loginuser", account);
            if(accountService.getAuthority(account) > 0){
                return "redirect:/main.html";
            }else {
                return "index_staff.html";
            }
        }else{
            model.addAttribute("msg", "用户名或密码错误！");
            return "login.html";
        }
    }

    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }
}
